﻿using Com.Lancetinc.Evs.Data;
using Com.Lancetinc.Evs.Services;
using Com.Lancetinc.Evs.ViewModels;
using System;
using System.Diagnostics;
using System.Windows;
using Com.Lancetinc.Evs.Data.Domain;
using Com.Lancetinc.Evs.Helpers;

namespace Com.Lancetinc.Evs.Windows
{
    /// <summary>
    /// ExperDetailWindow.xaml 的交互逻辑
    /// </summary>
    public partial class ExperDetailWindow
    {
        private ExperInfo mExper;
        private bool _changed = false;
        public ExperDetailWindow()
        {
            ThemeHelper.ChangeThemeToDark(this);

            InitializeComponent();
            var rc = SystemParameters.WorkArea;
            Width = Math.Min(rc.Width, 1440);
            Height = Math.Min(rc.Height, 960);
            ctrlVideo.OnFullScreenChanged += CtrlVideo_OnFullScreenChanged;
            GridTitle.MouseDown += GridTitle_MouseDown;
            this.Closing += ExperDetailWindow_Closing;
        }

        private void ExperDetailWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            // mExper == null表示被删除
            if (mExper != null && !mExper.Exper.IsReadonly)
            {
                try
                {
                    if (!BeanUtil.AreEqual(_experBk, mExper.Exper.Exper))
                    {
                        AppRepositories.Exper.Save(mExper.Exper.Exper);
                        _changed = true;
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
            }

            this.DialogResult = _changed;
        }

        public static bool Show(string seqNo)
        {
            var exper = AppRepositories.Exper.FindBySeqNo(seqNo);
            return Show(new ExperViewModel(exper));
        }

        public static bool Show(ExperViewModel exper)
        {
            var win = new ExperDetailWindow
            {
                Owner = Application.Current.MainWindow,
                mExper = ExperManager.Default.WrapHistoryExper(exper)
            };
            return win.ShowDialog() == true;
        }

        private void CtrlVideo_OnFullScreenChanged(object sender, bool e)
        {
            ctrlPatient.Visibility = e ? Visibility.Collapsed : Visibility.Visible;
        }

        private Exper _experBk = null;
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            ctrlPatient.DataContext = mExper.Exper;
            ctrlVideo.SetExper(mExper);
            ctrlPatient.CanDelete = true;
            _experBk = BeanUtil.Clone(mExper.Exper.Exper);
        }

        private void GridTitle_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        private void CtrlPatient_OnExperDeleted(object sender, EventArgs e)
        {
            mExper.Exper.SeqNo = null;
            mExper = null;
            _changed = true;
            Close();
        }

        private void CtrlPatient_OnSaveSucceed(object sender, EventArgs e)
        {
            _experBk = BeanUtil.Clone(mExper.Exper.Exper);
            _changed = true;
        }
    }
}